package com.fjec.market.schedule;

import com.fjec.market.schedule.handler.*;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
public class MarketSchedule {

    @Resource
    private PurchaseShopNumHandler purchaseShopNumHandler;

    @Resource
    private BusinessShopNumHandler businessShopNumHandler;

    @Resource
    private FarmerShopNumHandler farmerShopNumHandler;

    @Resource
    private SpuStarHandler spuStarHandler;

    @Resource
    private ShopStarHandler shopStarHandler;

    @Resource
    private PhyOrderTimeoutHandler phyOrderTimeoutHandler;

    @Resource
    private YsRegisterHandler ysRegisterHandler;

    @Resource
    private YsRegisterResultQueryHandler ysRegisterResultQueryHandler;

    @Resource
    private RequestPhyDivideHandler requestPhyDivideHandler;

    @Resource
    private RequestServDivideHandler requestServDivideHandler;

    @Resource
    private YsRegResult99Handler ysRegResult99Handler;

    @Autowired
    private AutoSignHandler autoSignHandler;

    @Autowired
    private AutoRefundHandler autoRefundHandler;

    /**
     * ShopNum生成补偿
     *
     * @param param
     * @return
     */
    @XxlJob("shopNumGenerateHandler")
    public ReturnT<String> shopNumGenerateHandler(String param) {
        // 处理参数
        String[] args = param.split(",");
        int limit = Integer.valueOf(args[0]).intValue();
        int minutes = Integer.valueOf(args[1]).intValue();
        int fnum = 0;
        int pnum = 0;
        int bnum = 0;
        // 处理果农
        try {
            fnum = farmerShopNumHandler.handle(limit, minutes);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            XxlJobLogger.log(ex);
        }
        // 处理采购商
        try {
            pnum = purchaseShopNumHandler.handle(limit, minutes);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            XxlJobLogger.log(ex);
        }
        // 处理分拣、农资、包装
        try {
            bnum = businessShopNumHandler.handle(limit, minutes);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            XxlJobLogger.log(ex);
        }
        // 记录日志
        String log = String.format("FARMER=%d, BIZ=%d, PUR=%d", fnum, bnum, pnum);
        XxlJobLogger.log(log);
        ReturnT<String> returnT = ReturnT.SUCCESS;
        returnT.setMsg(log);
        returnT.setContent("shopNumGenerateHandler");
        return returnT;
    }

    /**
     * 商品评分计算
     *
     * @return
     */
    @XxlJob("spuStarHandler")
    public ReturnT<String> spuStarHandle(String param) {
        // 处理
        int handleCount = spuStarHandler.handle();
        // 记录日志
        String log = String.format("Handle count: %d", handleCount);
        XxlJobLogger.log(log);
        ReturnT<String> returnT = ReturnT.SUCCESS;
        returnT.setMsg(log);
        returnT.setContent("spuStarHandler");
        return returnT;
    }

    /**
     * 店铺评分计算
     *
     * @return
     */
    @XxlJob("shopStarHandler")
    public ReturnT<String> shopStarHandler(String param) {
        // 处理
        String result = shopStarHandler.handle();
        // 记录日志
        XxlJobLogger.log(result);
        ReturnT<String> returnT = ReturnT.SUCCESS;
        returnT.setMsg(result);
        returnT.setContent("shopStarHandler");
        return returnT;
    }

    /**
     * 订单过期处理补偿线程
     *
     * @return
     */
    @XxlJob("orderTimeoutHandler")
    public ReturnT<String> orderTimeoutHandler(String param) {
        // 处理参数
        String[] args = param.split(",");
        int limit = Integer.valueOf(args[0]).intValue();
        int minutes = Integer.valueOf(args[1]).intValue();
        // 处理
        String log = phyOrderTimeoutHandler.handle(limit, minutes);
        // 记录日志
        XxlJobLogger.log(log);
        ReturnT<String> returnT = ReturnT.SUCCESS;
        returnT.setMsg(log);
        returnT.setContent("orderTimeoutHandler");
        return returnT;
    }

    /**
     * 银盛进件补偿
     *
     * @return
     */
    @XxlJob("ysRegisterHandler")
    public ReturnT<String> ysRegisterHandler(String param) {
        // 处理参数
        String[] args = param.split(",");
        int limit = Integer.valueOf(args[0]).intValue();
        int minutes = Integer.valueOf(args[1]).intValue();
        // 处理
        String log = ysRegisterHandler.handle(limit, minutes);
        // 记录日志
        XxlJobLogger.log(log);
        ReturnT<String> returnT = ReturnT.SUCCESS;
        returnT.setMsg(log);
        returnT.setContent("ysRegisterHandler");
        return returnT;
    }

    /**
     * 银盛进件出现身份证注册次数补偿
     *
     * @return
     */
    @XxlJob("ysRegResult99Handler")
    public ReturnT<String> ysRegResult99Handler(String param) {
        // 处理参数
        String[] args = param.split(",");
        int limit = Integer.valueOf(args[0]).intValue();
        int minutes = Integer.valueOf(args[1]).intValue();
        // 处理
        String log = ysRegResult99Handler.handle(limit, minutes);
        // 记录日志
        XxlJobLogger.log(log);
        ReturnT<String> returnT = ReturnT.SUCCESS;
        returnT.setMsg(log);
        returnT.setContent("ysRegResult99Handler");
        return returnT;
    }

    @XxlJob("ysRegisterResultQueryHandler")
    public ReturnT<String> ysRegisterResultQueryHandler(String param) {
        String[] args = param.split(",");
        int limit = Integer.valueOf(args[0]).intValue();
        int minutes = Integer.valueOf(args[1]).intValue();
        ysRegisterResultQueryHandler.handle(limit, minutes);
        return ReturnT.SUCCESS;
    }

    /**
     * 生成分账信息，获取已签收25小时后，无投诉 或 有投诉已处理 的订单，写到paid服务分账表
     *
     * @param param
     * @return
     */
    @XxlJob("requestDivideHandler")
    public ReturnT<String> requestPhyDivideHandler(String param) {
        String[] args = param.split(",");
        int limit = Integer.valueOf(args[0]).intValue();
        int minutes = Integer.valueOf(args[1]).intValue();
        String log = requestPhyDivideHandler.handle(limit, minutes);

        XxlJobLogger.log(log);
        ReturnT<String> returnT = ReturnT.SUCCESS;
        returnT.setMsg(log);
        returnT.setContent("requestDivideHandler");
        return returnT;
    }

    @XxlJob("requestServDivideHandler")
    public ReturnT<String> requestServDivideHandler(String param) {
        String[] args = param.split(",");
        int limit = Integer.valueOf(args[0]).intValue();
        int minutes = Integer.valueOf(args[1]).intValue();
        String log = requestServDivideHandler.handle(limit, minutes);

        XxlJobLogger.log(log);
        ReturnT<String> returnT = ReturnT.SUCCESS;
        returnT.setMsg(log);
        returnT.setContent("requestServDivideHandler");
        return returnT;
    }

    /**
     * 发货5天后自动收货
     *
     * @param param
     * @return
     */
    @XxlJob("autoSignHandler")
    public ReturnT<String> autoSignHandler(String param) {
        String[] args = param.split(",");
        int limit = Integer.valueOf(args[0]).intValue();
        int minutes = Integer.valueOf(args[1]).intValue();
        String log = autoSignHandler.handle(limit, minutes);

        XxlJobLogger.log(log);
        ReturnT<String> returnT = ReturnT.SUCCESS;
        returnT.setMsg(log);
        returnT.setContent("autoSignHandler");
        return returnT;
    }

    /**
     * 买家付款后 48小时卖家没发货，自动退款
     *
     * @param param
     * @return
     */
    @XxlJob("autoRefundHandler")
    public ReturnT<String> autoRefundHandler(String param) {
        String[] args = param.split(",");
        int limit = Integer.valueOf(args[0]).intValue();
        int minutes = Integer.valueOf(args[1]).intValue();
        String log = autoRefundHandler.handle(limit, minutes);

        XxlJobLogger.log(log);
        ReturnT<String> returnT = ReturnT.SUCCESS;
        returnT.setMsg(log);
        returnT.setContent("autoRefundHandler");
        return returnT;
    }

    @Autowired
    private YsDeliveryHandler ysDeliveryHandler;

    /**
     * 通知银盛卖家发货
     * @param param
     * @return
     */
    @XxlJob("ysDeliveryHandler")
    public ReturnT<String> ysDeliveryHandler(String param) {
        String[] args = param.split(",");
        int limit = Integer.valueOf(args[0]).intValue();
        String log = ysDeliveryHandler.handle(limit);

        XxlJobLogger.log(log);
        ReturnT<String> returnT = ReturnT.SUCCESS;
        returnT.setMsg(log);
        returnT.setContent("ysDeliveryHandler");
        return returnT;
    }

    @Autowired
    private YsSignHandler ysSignHandler;
    /**
     * 通知银盛买家确认收货
     * @param param
     * @return
     */
    @XxlJob("ysSignHandler")
    public ReturnT<String> ysSignHandler(String param) {
        String[] args = param.split(",");
        int limit = Integer.valueOf(args[0]).intValue();
        String log = ysSignHandler.handle(limit);

        XxlJobLogger.log(log);
        ReturnT<String> returnT = ReturnT.SUCCESS;
        returnT.setMsg(log);
        returnT.setContent("ysDeliveryHandler");
        return returnT;
    }

    @Resource
    private TmpUpdateShopWeightHandler tmpUpdateShopWeightHandler;

    /**
     * 临时定时更新店铺排序
     *
     * @param param 随机数最大值
     * @return
     */
    @XxlJob("tmpUpdateShopWeightHandler")
    public ReturnT<String> tmpUpdateShopWeightHandler(String param) {
        int num = Integer.valueOf(param).intValue();
        String log = tmpUpdateShopWeightHandler.handle(num);
        XxlJobLogger.log(log);
        ReturnT<String> returnT = ReturnT.SUCCESS;
        returnT.setMsg(log);
        returnT.setContent("tmpUpdateShopWeightHandler");
        return returnT;
    }


}
